<template>
  <div>
    <el-dialog
      :title="title + '节点'"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      width="600px"
      @closed="resetForm('form')"
    >
      <div>
        <el-form ref="form" :rules="rules" :model="form" label-width="80px">
          <el-form-item label="名称" prop="name">
            <el-input
              v-model.trim="form.name"
              v-clearIpt="form.name"
            ></el-input>
          </el-form-item>
          <el-form-item label="审核类型">
            <el-radio-group v-model="form.deptType">
              <el-radio label="R">角色</el-radio>
              <!-- <el-radio label="U">人员</el-radio> -->
            </el-radio-group>
          </el-form-item>
          <el-form-item
            label="审核人员"
            prop="expertsUserList"
            v-show="form.deptType == 'U'"
          >
            <div class="user-wrap">
              <p v-show="checkedUser.length == 0">请选择人员</p>
              <el-tag
                v-for="tag in checkedUser"
                :key="tag.userName"
                closable
                disable-transitions
                @close="handleClose(tag)"
              >
                {{ tag.userName }}
              </el-tag>
            </div>
            <el-button
              class="user-select-button"
              icon="el-icon-user-solid"
              type="primary"
              plain
              @click="handleSelect()"
            >
              选择人员
            </el-button>
          </el-form-item>
          <el-form-item
            label="审核角色"
            v-show="form.deptType == 'R'"
            prop="auditor"
          >
            <RoleSelect :defaultValue="form.auditor" @emit="setRoleId" />
          </el-form-item>
          <el-form-item label="描述">
            <el-input
              type="textarea"
              :rows="2"
              placeholder="请输入描述"
              v-model="form.des"
            >
            </el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('form')">确 认</el-button>
      </span>
    </el-dialog>
    <UserSelect
      v-if="dialogVisible"
      :initialValue="initialValue"
      @e-userData="setUserId"
    />
  </div>
</template>
<script>
import {
  flowDefinitionNodeAdd,
  flowDefinitionNodeUpdate,
} from '@/api/admin/audit/flowDefinitionNode.js'
import Bus from '@/utils/bus.js'
export default {
  components: {
    UserSelect: () => import('@/components/userSelect'),
    RoleSelect: () => import('@/components/dataSelect/roleSelect'),
  },
  data() {
    return {
      title: '新增',
      dialogVisible: false,
      addOrEdit: 1,
      checkedData: {},
      form: {
        deptType: 'R',
        name: '',
        auditor: '', //审核角色
        auditorList: [],
        // extFiled: "", //协同角色
        auditRelation: 'or', //审核关系;1-and,2-or
        definitionCode: '', //流程定义编码
        des: '',
      },
      rules: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
      },
      initialValue: {
        userId: [],
      },
      checkedUser: [],
    }
  },
  watch: {
    checkedUser(v) {
      this.initialValue.userId = JSON.parse(JSON.stringify(v))
    },
  },
  props: ['flowData'],
  mounted() {
    Bus.$on('showDialogFlowNodeAdd', (v) => {
      if (v) {
        this.checkedData = v
        this.handleAddOrEdit(2)
      } else {
        this.handleAddOrEdit(1)
      }
    })
  },
  beforeDestroy() {
    Bus.$off('showDialogFlowNodeAdd')
  },
  methods: {
    setRoleId(v) {
      this.form.auditor = v
    },
    setUserId(v) {
      let ids = this.checkedUser.map((v) => v.id)
      v.userId.forEach((item) => {
        if (ids.indexOf(item.id) == -1) {
          this.checkedUser.push(item)
        }
      })
    },
    handleClose(row) {
      this.checkedUser = this.checkedUser.filter((v) => v.id != row.id)
    },
    //显示弹窗
    handleAddOrEdit(type) {
      if (type == 2) {
        this.title = '编辑'
        for (let i in this.checkedData) {
          if (this.form.hasOwnProperty(i)) {
            this.form[i] = this.checkedData[i] || ''
          }
        }
        this.form.deptType = this.checkedData.deptType || 'U'
        if (this.checkedData.deptType == 'U') {
          this.form.auditor = ''
          this.checkedUser = [...this.checkedData.auditorList]
        }
        this.form.id = this.checkedData.id
      } else {
        this.title = '新增'
        delete this.form.id
      }
      this.addOrEdit = type
      this.dialogVisible = true
    },
    //提交
    submitForm(form) {
      this.form.auditorList = JSON.parse(JSON.stringify(this.checkedUser))
      this.form.definitionCode = this.flowData.code
      let { deptType, auditor, auditorList } = this.form
      if (deptType == 'U' && !auditorList.length) {
        this.$message({
          message: '请选择审核人员',
          type: 'warning',
        })
        return
      }
      if (deptType == 'R' && !auditor) {
        this.$message({
          message: '请选择审核角色',
          type: 'warning',
        })
        return
      }
      if (deptType == 'U') {
        this.form.auditor = ''
      } else {
        this.form.auditorList = []
      }
      this.$refs[form].validate((valid) => {
        if (valid) {
          this.addOrEdit == 1 ? this.add() : this.edit()
        }
      })
    },
    //新增
    add() {
      flowDefinitionNodeAdd(this.form).then((res) => {
        if (!res || res.data.code != 0) return
        this.dialogVisible = false
        Bus.$emit('refreshFlowNodeList')
        this.$message({
          message: '操作成功',
          type: 'success',
        })
      })
    },
    //编辑
    edit() {
      flowDefinitionNodeUpdate(this.form).then((res) => {
        if (!res || res.data.code != 0) return
        this.dialogVisible = false
        Bus.$emit('refreshFlowNodeList')
        this.$message({
          message: '操作成功',
          type: 'success',
        })
      })
    },
    handleSelect() {
      Bus.$emit('userSelect')
    },
    //重置表单
    resetForm(form) {
      this.$refs[form].resetFields()
      this.form.name = ''
      this.form.auditor = ''
      this.form.auditRelation = 'or'
      this.form.definitionCode = ''
      this.form.extFiled = ''
      this.form.des = ''
      this.checkedUser = []
      this.form.deptType = 'R'
    },
  },
}
</script>
<style scoped>
.user-wrap {
  display: inline-block;
  width: 350px;
  border: 1px solid #dcdfe6;
  border-radius: 5px;
  overflow: hidden;
  min-height: 40px;
  cursor: not-allowed;
}
.user-wrap p {
  line-height: 40px;
  color: #bbb;
  text-indent: 10px;
}
.user-wrap span {
  margin: 0 5px;
}
.user-select-button {
  float: right;
  padding-right: 10px;
  cursor: pointer;
}
.user-select-button span {
  font-size: 28px;
  color: #930b0b;
}
</style>